package com.cloudwise.controller;

import cn.hutool.core.util.ReflectUtil;
import com.cloudwise.entity.Dept;
import com.cloudwise.entity.PageBean;
import com.cloudwise.entity.ReturnBean;
import com.cloudwise.entity.User;
import com.cloudwise.util.ReturnStatusEnum;
import org.apache.shiro.SecurityUtils;

import java.util.Date;

/**
 * @author ：Teacher陈
 * @date ：Created in 2020/12/14 15:27
 * @description：所有controller的父类
 * @modified By：
 * @version: 1
 */
public class BaseController {

    /**
     * @create by: Teacher陈
     * @description: 通用分页参数工具
     * @create time: 2020/12/14 15:27
     * @param pageBean
     * @return   offset
     */
      public int calcPageParam(PageBean pageBean){
          int offset=(pageBean.getPage()-1)*pageBean.getLimit();
          return offset;
      }

      /**
       * @create by: Teacher陈
       * @description: 返回成功的结果
       * @create time: 2020/12/14 15:31
       * @param data
       * @return returnBean
       */
        public ReturnBean returnSuccess(Object data){

            ReturnBean returnBean= new ReturnBean();
            //设置返回状态码
            returnBean.setCode(ReturnStatusEnum.SUCCESS.getCode());
            //设置返回提示信息
            returnBean.setMsg(ReturnStatusEnum.SUCCESS.getMessage());
            //设置返回数据
            returnBean.setData(data);
            return returnBean;
        }
      /**
       * @create by: Teacher陈
       * @description: 返回失败的结果
       * @create time: 2020/12/14 15:31
       * @return  returnBean
       */
      public ReturnBean returnFail(Object data){
          ReturnBean returnBean= new ReturnBean();
          returnBean.setCode(ReturnStatusEnum.FAIL.getCode());
          returnBean.setMsg(ReturnStatusEnum.FAIL.getMessage());
          //设置返回数据
          returnBean.setData(data);
          return returnBean;
      }


    /**
     * @create by: Teacher陈
     * @description: 返回失败的结果
     * @create time: 2020/12/14 15:31
     * @return  returnBean
     */
    public ReturnBean returnFailMessage(Object data,String message){
        ReturnBean returnBean= new ReturnBean();
        returnBean.setCode(ReturnStatusEnum.FAIL.getCode());
        returnBean.setMsg(message);
        //设置返回数据
        returnBean.setData(data);
        return returnBean;
    }

      /**
       * @create by: Teacher陈
       * @description: 统一处理创建人，创建时间
       * @create time: 2020/12/22 10:55
       * @return
       */
      public <T> T setCreate(T t){
          //shiro框架获取登录用户信息
          User user = (User) SecurityUtils.getSubject().getPrincipal();
          //hutool工具调用方法
          ReflectUtil.invoke(t, "setCreateBy", user.getLoginName());
          ReflectUtil.invoke(t, "setCreateTime", new Date());
          return t;
      }
      /**
       * @create by: Teacher陈
       * @description: 统一处理修改人，修改时间
       * @create time: 2020/12/22 10:55
       * @return
       */
      public <T> T setUpdate(T t){
          //shiro框架获取登录用户信息
          User user = (User) SecurityUtils.getSubject().getPrincipal();
          //hutool工具调用方法
          ReflectUtil.invoke(t, "setUpdateBy", user.getLoginName());
          ReflectUtil.invoke(t, "setUpdateTime", new Date());
          return t;
      }
}
